package com.cmw.auntapp.core.util;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import android.content.Context;
import android.media.MediaPlayer;
import android.net.Uri;
import android.view.SurfaceHolder;
import android.webkit.URLUtil;

import com.cmw.auntapp.service.LocationService;

/**
 * @desc: 音频播放工具类
 * @author: chengmingwei
 * @date: 2021/5/23 1:39 PM
 * @contact: phone(微信同号):13922111489 / email: 340360491@qq.com
 */
public class MediaPlayerUtil {
    public static final int MEDIA_STATUS_START = 0;
    public static final int MEDIA_STATUS_PLAY = 1;
    public static final int MEDIA_STATUS_STOP = 2;
    public static final int MEDIA_STATUS_PAUSE = 3;


    public static MediaPlayer create(byte[] mediaBytes) {
        MediaPlayer mediaplayer = null;
        try {
            File temp = File.createTempFile("voice", "wav");
            FileOutputStream fos = new FileOutputStream(temp);
            fos.write(mediaBytes);
            fos.flush();
            fos.close();
            String tempPath = temp.getAbsolutePath();
            mediaplayer = new MediaPlayer();
            mediaplayer.setDataSource(tempPath);
            // mediaplayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
            mediaplayer.prepare();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return mediaplayer;
    }

    public static MediaPlayer create(Context context, File file) {
        MediaPlayer mediaplayer = null;
        try {
            mediaplayer = new MediaPlayer();
            mediaplayer.setDataSource(file.getAbsolutePath());
            mediaplayer.prepare();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return mediaplayer;
    }

    // url or file path
    public static MediaPlayer create(Context context, String path) {
        if (URLUtil.isNetworkUrl(path)) {
            Uri playUri = Uri.parse(path);
            return create(context, playUri);
        } else {
            MediaPlayer mediaplayer = null;
            try {
                mediaplayer = new MediaPlayer();
                mediaplayer.setDataSource(path);
                mediaplayer.prepare();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return mediaplayer;
        }

    }

    public static MediaPlayer create(Context context, InputStream stream) {
        MediaPlayer mediaplayer = null;
        try {
            File temp = File.createTempFile("voice", "wav");
            String tempPath = temp.getAbsolutePath();
            FileOutputStream fos = new FileOutputStream(temp);
            BufferedOutputStream bos = new BufferedOutputStream(fos);
            byte buf[] = new byte[1024];
            do {
                int numread = stream.read(buf);
                if (numread <= 0)
                    break;
                bos.write(buf, 0, numread);
            } while (true);
            mediaplayer = new MediaPlayer();
            mediaplayer.setDataSource(tempPath);
            mediaplayer.prepare();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return mediaplayer;
    }

    public static MediaPlayer create(Context context, int resid) {
        return MediaPlayer.create(context, resid);
    }

    public static MediaPlayer create(Context context, Uri uri) {
        return MediaPlayer.create(context, uri);
    }

    public static MediaPlayer create(Context context, Uri uri,
                                     SurfaceHolder holder) {
        return MediaPlayer.create(context, uri, holder);
    }

    public static void releaseMediaPlayer(MediaPlayer mMediaPlayer) {
        if (mMediaPlayer != null) {
            if (mMediaPlayer.isPlaying()) {
                mMediaPlayer.stop();
            }
            mMediaPlayer.release();
            mMediaPlayer = null;
        }
    }

    /**
     * 播放音乐
     *
     * @param context Context 对象
     * @param resId 音频文件资源ID
     * @param looping   是否循环播放 [true:循环播放, false: 不循环播放]
     * @return 返回 MediaPlayer 对象
     */
    public static MediaPlayer playerMusic(Context context, int resId, boolean looping) {
        MediaPlayer mediaPlayer = create(context, resId);
        mediaPlayer.setLooping(looping);
        mediaPlayer.start();
        return mediaPlayer;
    }
}
